package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.entity.Customer;
import cn.yj.cgs.dba.entity.vo.CustomerPcVo;
import cn.yj.cgs.dba.entity.vo.InviteesRanking;
import cn.yj.cgs.dba.mapper.CustomerMapper;
import cn.yj.common.NameValue;
import cn.yj.commons.utils.MapUtils;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-09 15:29
 */
@Service
public class CustomerService extends ServiceImpl<CustomerMapper, Customer> {
    public Page<CustomerPcVo> pageList(@Require Map<String, Object> params, @Require Page<CustomerPcVo> page) {
        baseMapper.findList(params, page);
        return page;
    }

    @CheckObjectValue(keyValue = @KeyValue(type = Map.class, name = {"userCode","status"}))
    public boolean changeUserStatus(Map<String, Object> params) {
        Integer status = MapUtils.getIntVal(params,"status");
        String userCode = MapUtils.getStringVal(params,"userCode");
        return super.update( new Customer().setUserCode(userCode)
                .setUserStatus(status),new UpdateWrapper<Customer>().lambda().eq(Customer::getUserCode,userCode));
    }

    public Page<InviteesRanking> inviteesRankingList(Map<String, Object> params,@Require Page<InviteesRanking> page) {
        baseMapper.inviteesRankingList(params, page);
        return page;
    }

    public long countByDate(Date date){
        return baseMapper.countUserByDate(date);
    }

    public long countByDateRange(Date startTime,Date endTime){
        return baseMapper.countUserByDateRange(startTime,endTime);
    }

    public long countUser(){
        return baseMapper.countAllUser();
    }

    public List<NameValue> pieChart() {
        return baseMapper.pieChart();
    }
}
